package com.itheima.health.controller;


import com.itheima.health.common.MessageConst;
import com.itheima.health.entity.PageResult;
import com.itheima.health.entity.QueryPageBean;
import com.itheima.health.entity.Result;
import com.itheima.health.pojo.CheckGroup;
import com.itheima.health.pojo.CheckItem;
import com.itheima.health.service.CheckGroupService;
import com.itheima.health.service.CheckItemService;
import com.mysql.cj.protocol.x.MessageConstants;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/checkgroup")
@Slf4j
//检查组管理
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;
    @Autowired
    private CheckItemService checkItemService;

    //分页查询
    @GetMapping("/findPage")
    public Result page(QueryPageBean queryPageBean){
        PageResult pageResult = checkGroupService.pageQuery(queryPageBean);
        return new Result(true,MessageConst.ACTION_SUCCESS,pageResult);
    }

    //新增检查组
    @PostMapping("/add")
    public Result add(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        log.info("新增检查组{},{}",checkGroup,checkitemIds);
        checkGroupService.add(checkGroup,checkitemIds);
        return new Result(true,MessageConst.ADD_CHECKITEM_SUCCESS);
    }

    //删除检查组
    @PostMapping("/deleteCheckGroupitemById")
    public Result deleteCheGroupitemById(Integer id){
        log.info("删除检查组:{}",id);
        checkGroupService.deleteById(id);
        return new Result(true,MessageConst.DELETE_CHECKGROUP_SUCCESS);
    }

    //回显检查组
    @GetMapping("/findById")
    public Result findById(Integer id){
        log.info("回显检查组:{}",id);
        CheckGroup checkGroup = checkGroupService.findById(id);
        return new Result(true,MessageConst.EDIT_CHECKGROUP_SUCCESS,checkGroup);
    }

    //回显检查项
    @GetMapping("/findCheckItemIdsByCheckGroupId")
    public Result findCheckItemIdsByCheckGroupId(Integer id){
        log.info("回显检查项:{}",id);
        List<Integer> list = checkGroupService.findCheckItemId(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,list);
    }

    //编辑检查组
    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,Integer[] checkitemIds){
        log.info("编辑检查组:{},{}",checkGroup,checkitemIds);
        checkGroupService.edit(checkGroup,checkitemIds);
        return new Result(true,MessageConst.ACTION_SUCCESS);
    }

    //回显套餐中的检查组
    @GetMapping("/findAll")
    public Result findAll(){
        log.info("回显套餐中的检查组:{}");
        List<CheckGroup> checkGroup = checkGroupService.findAll();
        return new Result(true,MessageConst.ADD_SETMEAL_SUCCESS,checkGroup);
    }

    //回显检查组
    @GetMapping("/findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(Integer id){
        log.info("回显检查项:{}",id);
        List<Integer> list = checkGroupService.findChegroupId(id);
        return new Result(true,MessageConst.ACTION_SUCCESS,list);
    }
}
